<?php
class TestConclusions extends CActiveRecord {
	
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	public function tableName() {
		return strtolower('PsyTestConclusions');
	}

    public function defaultScope() {
        return array(
            'order' => 'conclusionOrder ASC'
        );
    }

    public function relations() {
        return array(
            'topic' => array(self::BELONGS_TO, 'TestTopics', 'topicId',
                'joinType'=> 'RIGHT JOIN',
            ),
		);
    }
	
	public function rules() {
		return array(
            array('conclusionDescr, conclusionOrder, conclusionSum', 'required'),
            array('formula', 'required', 'on' => 'forMarkTopic'), 
            array('conclusionOrder', 'numerical')
		);
	}

    public function updateScenario() {
        if($this->getTopicCatalog() == 'mark') {
            $this->scenario = 'forMarkTopic';
        } else if($this->getTopicCatalog() == 'jumping') {
            $this->scenario = 'forJumpingTopic';
        }
    }
    
    public function afterFind() {
    	$this->updateScenario();
    }
    
    public function afterDelete() {
        if($this->getTopicCatalog() == 'jumping') {
    	    TestAnswerLinkage::model()->deleteAll('linkToId = '.$this->conclusionId.' AND linkType = "'.TestAnswerLinkage::LINKTYPE_CONCLUSION.'"');
        }
    }

    public function getTopicCatalog() {
	if($this->topic !== null) {
        	return $this->topic->getTypeCatalog();
	}
    }

    public function isNeedFormula() {
        return $this->scenario == 'forMarkTopic';
    }

    public function attributeLabels() {
        return array(
            'formula' => '公式',
            'conclusionDescr' => '结论描述',
            'conclusionOrder' => '结论序号',
            'conclusionSum' => '结论概括'
        );
    }
}
